(function($){

    function SelectFile(config){
        var self = this;
        $.extend(this,config);
        return this.init(config);
    }

    SelectFile.prototype = {
        constructor: 'SelectFile',
        init : function(){
            this.$wrapper = $(this.container);
            this.bindEvent();
        	this.dragging = false;
        },
        bindEvent : function(){
        	var self = this;
        	this.$wrapper.get(0).ondragstart = this.dragStart.bind(this);
            this.$wrapper.get(0).ondragenter = this.dragEnter.bind(this);
            this.$wrapper.get(0).ondragleave = this.dragLeave.bind(this);
            this.$wrapper.get(0).ondragover = this.dragOver.bind(this);
            this.$wrapper.get(0).ondrop = this.drop.bind(this);
            
            $(document).get(0).ondragover = function(e){
        		e.stopPropagation();  
            	e.preventDefault(); 
        	}
        	$(document).get(0).ondrop = function(e){
        		e.stopPropagation();  
            	e.preventDefault(); 
        	}
        	if(this.localUpload){
        		this.localUploadFn();
        	}
        },
        dragStart : function(){

        },
        /**
         * 拖拽进入
         */
        dragEnter : function(e){
            this.$wrapper.addClass('enter');
        },
        /**
         * 拖拽离开
         */
        dragLeave : function(e){
            this.$wrapper.removeClass('enter');
        },
        /**
         * 拖拽进入
         */
        dragOver: function(e) {
        	e.stopPropagation();  
            e.preventDefault();
            this.$wrapper.addClass('enter');     
        },
        /**
         * 放下文件时
         */
        drop: function(e) {
        	var self = this;
        	var files = e.files || e.dataTransfer.files;
        	var dom = '',imgSrc=[],finish=false;
        	self.$wrapper.removeClass('enter');
        	
        	// drop时有e.dataTransfer
        	
	     //    	$.each(files,function(i,file){
	     //    		if(file.type.indexOf(this.type)!=-1){
	     //    			var fd = new FileReader();
						// fd.readAsDataURL( file );
						// fd.onload = function(){
						// 	if(i == files.length-1){
						// 		finish = true;
						// 	}
						// 	imgSrc.push(this.result);
						// };
	     //    		}else{
	     //    			alert(this.text.typeError);
	     //    		}
	     //    	});
        	
        		$.each(files,function(i,file){
        			imgSrc.push(self.createObjectURL(file));
	        	});
	        	
        		$.each(imgSrc,function(i,src){
	        		dom += self.tpl.replace(/{{imgSrc}}/ig,src);
	        	})
	        	self.$wrapper.append(dom);
                if(this.callback){
                    this.callback();
                }
        },
        /**
         * 生成url
         */
        createObjectURL: function(file) {　
            var url = null;
            if (window.webkitURL) {
                url = window.webkitURL.createObjectURL(file)
            } else if (window.URL) {
                url = window.URL.createObjectURL(file);
            }
            return url;
        },
        /**
         * 从本地选择图片
         */
        localUploadFn : function(){
       		var self = this;
        	var $localBtn = this.UploadBtn;
        	$localBtn.off('click').on('click',function(){
		       
		        var input = document.createElement('input');
		        input.type = 'file';
		        input.multiple = 'multiple';
		        input.onchange = function(e) {
		            e.files = this.files;
		            self.drop(e);
		            input.onchange = null;
		            input = null;
		        };
		        input.click();
		    })
        }
    }

    $.fn.selectfile = function(options) {
        var returnVal = this;
        //这里合并配置
        $.extend(true, $.fn.selectfile.defaults, options)
        this.each(function(key, the) {
            new SelectFile($.extend({ container: the }, $.fn.selectfile.defaults));
        })
        return returnVal;
    };

    $.fn.selectfile.defaults = {
        type: 'image',
        callback : '', //将内容填充进去之后的回调
        tpl:'<div class="pic-list-piece">\
				<img src="{{imgSrc}}" alt="">\
			</div>',
    	text : {
    		typeError : '仅支持上传图片'
    	},
    	localUpload : false,
    	UploadBtn : ''
    }

})(jQuery)